﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bastanteo.Dominio;
using Bastanteo.Negocio;

//mio mio mio
namespace Bastanteo.Controllers
{
    public class ClienteController : Controller
    {
        IAdministracionService adminService = null;
        //
        // GET: /Cliente/

        public ActionResult Index()
        {
         
            return View(adminService.ListarClientes());
        }

        public ActionResult Buscar(String ruc)
        {
            ICollection<Cliente> modelo = adminService.ListarXRuc(ruc);
            return View("Index", modelo);
        }
        //
        // GET: /Cliente/Details/5

        public ActionResult Details(int id)
        {
            return View(adminService.ObtenerCliente(id));
        }

        //
        // GET: /Cliente/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Cliente/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {


            try
            {
                string ruc = collection["Ruc"];
                string razonSocial = collection["RazonSocial"];
                DateTime inicio = DateTime.Parse(collection["Inicio"]);
                string tipoempresa = collection["TipoEmpresa"];
                int empleados = int.Parse(collection["Empleados"]);
                Cliente c = adminService.RegistrarCliente(ruc, razonSocial, inicio, tipoempresa, empleados);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Create");
            }
        }
        
        //
        // GET: /Cliente/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult Delete(Cliente cliente)
        {
            try
            {

                adminService.EliminarCliente(cliente); 
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Create");
            }
        }
    }
}
